/**
 * 
 */
package com.cap.cfae;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cap.cfae.cfae.CfaeIssuer;
import com.eos.das.entity.criteria.CriteriaType;
import com.eos.das.entity.criteria.ExprType;
import com.eos.das.entity.criteria.ExprType.OP;
import com.eos.foundation.database.DatabaseUtil;
import com.eos.system.annotation.Bizlet;

import commonj.sdo.DataObject;

/**
 * @author yangzhenzhou
 * @date 2018-02-26 22:10:51
 * 
 */
@Bizlet("")
public class IssuerService {
	@Bizlet("getIssuers")
	public Map<String, Object> getIssuers(String token) {
		System.out.print(token);
		Map<String, Object> issuers = new HashMap<String, Object>();
		CriteriaType criteria = CriteriaType.FACTORY.create();
		criteria.set_entity(CfaeIssuer.QNAME);
		List<ExprType> exprList = new ArrayList<ExprType>();
		ExprType exprName = ExprType.FACTORY.create();
		exprName.set_opEnum(OP.EQ);
		exprName.set_property("1");
		exprName.set_value("1");
		exprList.add(exprName);
		criteria.set_expr(exprList);
		criteria.set_expr(exprList);
		DataObject[] issuersData = DatabaseUtil.queryEntitiesByCriteriaEntity(
				"default", criteria);
		// System.out.print("issuersData:" + issuersData.length);
		List<Map<String, Object>> issersList = new ArrayList<Map<String, Object>>();

		for (int i = 0; i < 15; i++) {
			Map<String, Object> issuerObject;
			issuerObject = new HashMap<String, Object>();
			DataObject issuer = issuersData[i];
			if (issuer != null) {
				issuerObject.put("issuerid", issuer.getString("issueUserid"));
				issuerObject.put("issuer", issuer.getString("issueUser"));
				issuerObject.put("issuerGrade", issuer.getString("bondGrade"));
				issuerObject.put("issuerRegion",
						issuer.getString("issuerRegion"));
				issuerObject.put("issuerType", issuer.getString("issuerType"));
				issuerObject.put("issuerRegionBh",
						issuer.getString("issuerRegionBh"));
				issersList.add(issuerObject);
				// System.out.println("issuersData:" + i + "---"+
				// issuersData[i].getString("issueUser"));
			}
		}
		issuers.put("issuerList", issersList);
		return issuers;
	}

	@Bizlet("getIssuerByName")
	public Map<String, Object> getIssuerByName(String token, String issuerName) {
		Map<String, Object> result = new HashMap<String, Object>();
		CriteriaType criteria = CriteriaType.FACTORY.create();
		criteria.set_entity(CfaeIssuer.QNAME);
		List<ExprType> exprList = new ArrayList<ExprType>();
		ExprType exprName = ExprType.FACTORY.create();
		exprName.set_opEnum(OP.LIKE);
		exprName.set_property("issueUser");
		exprName.set_value(issuerName);
		exprList.add(exprName);
		criteria.set_expr(exprList);
		criteria.set_expr(exprList);
		DataObject[] issuersData = DatabaseUtil.queryEntitiesByCriteriaEntity(
				"default", criteria);
		// System.out.println("issuersData:"+ "---"+issuersData.length);
		List<Map<String, Object>> issuerList = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < issuersData.length; i++) {
			Map<String, Object> issuerObject;
			issuerObject = new HashMap<String, Object>();
			DataObject issuer = issuersData[i];
			if (issuer != null) {
				issuerObject.put("issuerid", issuer.getString("issueUserid"));
				issuerObject.put("issuer", issuer.getString("issueUser"));
				issuerObject.put("issuerGrade", issuer.getString("bondGrade"));
				issuerObject.put("issuerRegion",
						issuer.getString("issuerRegion"));
				issuerObject.put("issuerType", issuer.getString("issuerType"));
				issuerObject.put("issuerRegionBh",
						issuer.getString("issuerRegionBh"));
				issuerList.add(issuerObject);
			}
		}
		result.put("issuers", issuerList);
		return result;
	}

	@Bizlet("getIssuerByCompositeQuery")
	public Map<String, Object> getIssuerByCompositeQuery(String token,
			String issuerGrade, String issuerInstury, String issuerType,
			String issuerRegion) {

		Map<String, Object> result = new HashMap<String, Object>();
		CriteriaType criteria = CriteriaType.FACTORY.create();
		criteria.set_entity(CfaeIssuer.QNAME);
		List<ExprType> exprList = new ArrayList<ExprType>();
		// 主体评级
		if (!"".equals(issuerInstury)) {
			ExprType exprGrade = ExprType.FACTORY.create();
			exprGrade.set_opEnum(OP.EQ);
			exprGrade.set_property("issueGrade");
			exprGrade.set_value(issuerGrade);
			exprList.add(exprGrade);
		}

		// 行业
		// if(!"".equals(issuerInstury)){
		// ExprType exprInstury = ExprType.FACTORY.create();
		// exprInstury.set_opEnum(OP.EQ);
		// exprInstury.set_property("issueGrade");
		// exprInstury.set_value(issuerInstury);
		// exprList.add(exprInstury);
		// }
		// 企业类型
		if (!"".equals(issuerType)) {
			ExprType exprType = ExprType.FACTORY.create();
			exprType.set_opEnum(OP.EQ);
			exprType.set_property("issuerType");
			exprType.set_value(issuerType);
			exprList.add(exprType);
		}
		// 企业区域
		if (!"".equals(issuerRegion)) {
			ExprType exprRegion = ExprType.FACTORY.create();
			exprRegion.set_opEnum(OP.EQ);
			exprRegion.set_property("issuerRegion");
			exprRegion.set_value(issuerRegion);
			exprList.add(exprRegion);
		}
		criteria.set_expr(exprList);
		criteria.set_expr(exprList);
		DataObject[] issuersData = DatabaseUtil.queryEntitiesByCriteriaEntity(
				"default", criteria);
		// System.out.println("issuersData:"+ "---"+issuersData.length);
		List<Map<String, Object>> issuerList = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < 20; i++) {
			Map<String, Object> issuerObject;
			issuerObject = new HashMap<String, Object>();
			DataObject issuer = issuersData[i];
			if (issuer != null) {
				issuerObject.put("issuerid", issuer.getString("issueUserid"));
				issuerObject.put("issuer", issuer.getString("issueUser"));
				issuerObject.put("issuerGrade", issuer.getString("bondGrade"));
				issuerObject.put("issuerRegion",
						issuer.getString("issuerRegion"));
				issuerObject.put("issuerType", issuer.getString("issuerType"));
				issuerObject.put("issuerRegionBh",
						issuer.getString("issuerRegionBh"));
				issuerList.add(issuerObject);
			}
		}
		result.put("issuers", issuerList);
		return result;
	}

}
